﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using RiskMan.Config;
using RiskMan.DataClass.Models;
using RiskMan.DataClass.Stops;
using RiskMan.Views.Dialogs;

namespace RiskMan.Views.Stop
{
    /// <summary>
    /// Interaction logic for newStop.xaml
    /// </summary>
    public partial class NewStop : Window
    {
        private StopsModel Slm;
        List<Trade> trades;
        public NewStop(StopsModel slm)
        {
            InitializeComponent();
            Slm = slm;

            #region ClientCode
            trades = MainWindow.StaticLevel._dataProvider.GetTradeList();
            CmbxClientCode.ItemsSource = trades.Select(a => a.ClientCode).ToList();
            CmbxClientCode.SelectedIndex = 0;
            for (int i = 0; i < CmbxClientCode.Items.Count; i++)
            {
                if (CmbxClientCode.Items[i].ToString() == slm.ClientCode)
                    CmbxClientCode.SelectedIndex = i;
            } 
            #endregion

            #region Seccode
            CmbxInstrument.ItemsSource = MainWindow.StaticLevel._dataProvider.GetSeccodeList();
            CmbxInstrument.SelectedIndex = 0;
            for (int i = 0; i < CmbxInstrument.Items.Count; i++)
            {
                if (CmbxInstrument.Items[i].ToString() == slm.Instrument)
                    CmbxInstrument.SelectedIndex = i;
            }
            #endregion

            //#region TypeRacheta
            //CmbxTypeRacheta.ItemsSource = slm.TypePriceList;
            //CmbxTypeRacheta.SelectedIndex = 0;
            //for (int i = 0; i < CmbxTypeRacheta.Items.Count; i++)
            //{
            //    if (CmbxTypeRacheta.Items[i].ToString() == slm.TypeCalculation)
            //        CmbxTypeRacheta.SelectedIndex = i;
            //}
            //#endregion

            //#region TypeStop
            //CmbxTypeStop.ItemsSource = slm.TypeStopList;
            //CmbxTypeStop.SelectedIndex = 0;
            //for (int i = 0; i < CmbxTypeStop.Items.Count; i++)
            //{
            //    if (CmbxTypeStop.Items[i].ToString() == slm.TypeStop)
            //        CmbxTypeStop.SelectedIndex = i;
            //}
            //#endregion

            TxbxStopLimitOtst.Text = slm.StopLimitOtstup.ToString();
            TxbxStopLimitSpread.Text = slm.StopLimitSpred.ToString();
            TxbxBreakeven.Text = slm.StopLimitBreakeven.ToString();

        }

        private void BtnSave_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (CmbxClientCode.SelectedIndex > 0)
                {
                    Slm.ClientCode = CmbxClientCode.SelectedValue.ToString();
                    List<Trade> trades = MainWindow.StaticLevel._dataProvider.GetTradeList();

                    for (int i = 0; i < trades.Count; i++)
                        if (trades[i].ClientCode == Slm.ClientCode)
                            Slm.Account = trades[i].Account;
                }
                else
                {
                    new DialogOkCancel("Выберите код клиента(счет)!", "Ошибка добавления");
                    return;
                }

                if (CmbxInstrument.SelectedIndex > 0)
                    if (CmbxInstrument.SelectedValue.ToString() != "Не выбран")
                        Slm.Instrument = CmbxInstrument.SelectedValue.ToString();
                    else
                    {
                        new DialogOkCancel("Выберите торгуемый инструмент!", "Ошибка добавления");
                        return;
                    }
                else
                {
                    new DialogOkCancel("Выберите торгуемый инструмент!", "Ошибка добавления");
                    return;
                }

                Slm.TypeCalculation = CmbxTypeRacheta.SelectedItem.ToString();
                Slm.TypeStop = CmbxTypeStop.SelectedItem.ToString();

                Slm.StopLimitOtstup = Convert.ToDouble(TxbxStopLimitOtst.Text);
                Slm.StopLimitSpred = Convert.ToDouble(TxbxStopLimitSpread.Text);
                Slm.StopLimitBreakeven = Convert.ToDouble(TxbxBreakeven.Text);

                DialogResult = true;

                Close();
            }
            catch
            {
            }
        }

        private void BtnCancel_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
        }

        #region Обработка получение текстобксом фокуса и потери его, замена запятой на точку

        private void TxbxLostFocus(object sender, RoutedEventArgs e)
        {
            if (((TextBox)sender).Text == "")
                ((TextBox)sender).Text = 0.ToString();

            char pointChar = ',';
            string str = (1.2).ToString();
            if (str.IndexOf('.') > 0) pointChar = '.';

            if (pointChar == ',')
                ((TextBox)sender).Text = ((TextBox)sender).Text.Replace('.', pointChar);

            if (pointChar == '.')
                ((TextBox)sender).Text = ((TextBox)sender).Text.Replace(',', pointChar);


        }

        private void TxbxGotFocus(object sender, RoutedEventArgs e)
        {
            try
            {
                if (((TextBox)sender).Text != "")
                {
                    if (Convert.ToDouble(((TextBox)sender).Text) == 0)
                    {
                        ((TextBox)sender).Text = "";
                    }
                }
            }
            catch
            {
                ((TextBox)sender).Text = "";
            }
        }

        private void TxbxPreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            if (!Char.IsDigit(e.Text, 0))
                e.Handled = true;

            if ((e.Text.IndexOf('.') == 0 || e.Text.IndexOf(',') == 0) && ((TextBox)sender).Text.IndexOf('.') == -1 && ((TextBox)sender).Text.IndexOf(',') == -1)
                e.Handled = false;
        }

        private void TxbxDitgitPreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            if (!Char.IsDigit(e.Text, 0))
                e.Handled = true;
        }
        #endregion
    }
}
